
<!DOCTYPE html>
<html>
<head>
    <title>sql </title>
    <meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>
	<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>

<body>
<div class="container">
	<textarea id='sql' style='width:100%;height:200px;' ></textarea>
	<div class="row">
	  <div class="col-md-3">
		<div class="list-group">
		</div>
	  </div>
	  <div class="col-md-9">
	  	<div class="sql">
	  	</div>
	  </div>
  </div>
</div>
<script type="text/javascript">
	$(function() {
		$('#sql').change(function() {
			var text = $(this).val();
			var r = /(\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}).+\.(\S+\.\S+)?\s?\-==>\s+?Preparing:\s+?(.+)[\s\S]+?Parameters:\s+?(.+)/gi;
			var x;
			var i = 0, data = [];
			while ((x = r.exec(text)) != null) {
				$('.list-group').append('<a href="#" class="list-group-item" data="'+i+'"><h4 class="list-group-item-heading">'+x[2]+'</h4><p  class="list-group-item-text">'+x[1]+'</p></a>');
				var sql = x[3];
				var params = $.trim(x[4]);
				if (params != '') {
					params = params.split(',');
					$.each(params, function(i, param) {
						param = $.trim(param);
						if (param.endsWith('(String)')) {
							sql = sql.replace('?', '\''+param.substring(0, param.lastIndexOf('(String)'))+'\'');
						} else if (param.endsWith('(Timestamp)')) {
							sql = sql.replace('?', 'TO_DATE(\''+param.substring(0, param.lastIndexOf('(Timestamp)'))+'\', \'YYYY-MM-DD HH24:MI:SS\')');
						} else if (param == 'null') {
							sql = sql.replace('?', NULL);
						} else {
							sql = sql.replace('?', param.substring(0, param.lastIndexOf('(')));
						}
					});
				}
				data[i] = sql;
				i++;
			}

			$('.list-group a').click(function() {
				$('.sql').html('<div class="alert alert-info" role="alert">' + data[$(this).attr('data')] + '</div>');
			});
		});
		$('.alert').click(function() {
			$(this).select();
			document.execCommand('copy');
		});
	});
</script>
</body>